<?php
class Empeval_LpImport_EmployeeImport {
	
	public function start() {		
		$mapUnits = new Empeval_LpImport_EmployeeImport_Units();
		$mapPositions = new Empeval_LpImport_EmployeeImport_Positions();
		$mapUsers = new Empeval_LpImport_EmployeeImport_Users();
		$lpEmployees = LpEmployeesQuery::create()
		->useEmployeesPositionsQuery()
			->useUnitsQuery(null, Criteria::LEFT_JOIN)
				->leftJoinUnitsRelatedByParentId('ParentUnit')
			->endUse()
			->usePositionsQuery()
				->useUmowyStanowiskaQuery()
					->orderByStartDate(Criteria::ASC)
				->endUse()
			->endUse()
		->endUse()
		->with('EmployeesPositions')
		->with('Units')
		->with('ParentUnit')
		->with('Positions')
		->with('UmowyStanowiska')
		->find();
		
		$userRoleDefault = EmpevalRolesQuery::create()->filterByType(EmpevalRoles::TYPE_RANK)->findOneByCode(EmpevalRoles::CODE_USER);
		
		$con = Propel::getConnection('vgp_empeval');
		$con->beginTransaction();
		try {
			foreach ($lpEmployees as $lpEmployee) {
				$updatedUser = $mapUsers->update($lpEmployee);
		
				$lpUnit = $lpEmployee->getEmployeesPositions()->getUnits();
				$lpPosition = $lpEmployee->getEmployeesPositions()->getPositions();
				if ($lpPosition == null) break;
				
				$updatedUnit = $mapUnits->update($lpUnit);
				$updatedUnit->save($con);
				
				$updatedPosition = $mapPositions->update($lpPosition);
				$updatedPosition->save($con);
				
				$activeDate = ($lpPosition->getUmowyStanowiskas()->getFirst() instanceof LpUmowyStanowiska) ? $lpPosition->getUmowyStanowiskas()->getFirst()->getStartDate(null) : new DateTime('now');
				$deactiveDate = clone $activeDate;
				$deactiveDate->modify('-1 day');
				
				if ($updatedUser->isNew() || $updatedUser->getUserPositionss()->count() == 0) {
					$updatedUser->addUserPositions($this->_newUserPosition($updatedUser, $updatedPosition, $updatedUnit, $activeDate, $con));
				}
				else {					
					$activeUserPosition = $this->_getActiveUserPosition($updatedUser->getUserPositionss());
					if (!($activeUserPosition instanceof EmpevalUserPositions)) {
						$updatedUser->addUserPositions($this->_newUserPosition($updatedUser, $updatedPosition, $updatedUnit, $activeDate, $con));
					}
					else if ($activeUserPosition->getPositionId() != $updatedPosition->getId()) {
						$activeUserPosition->setActive(false);
						$activeUserPosition->setDeactiveDate($deactiveDate->format('Y-m-d'));
						EmpevalUserPositionsQuery::create()
							->filterByUsers($updatedUser)
							->filterByActiveDate($activeUserPosition->getActiveDate())
							->update(array('DeactiveDate' => $deactiveDate->format('Y-m-d')), $con);
						$updatedUser->addUserPositions($this->_newUserPosition($updatedUser, $updatedPosition, $updatedUnit, $activeDate, $con));
					}
					else if ($activeUserPosition->getUnitId() != $updatedUnit->getId()) {
						$activeUserPosition->setActive(false);
						$updatedUser->addUserPositions($this->_newUserPosition($updatedUser, $updatedPosition, $updatedUnit, $activeDate, $con));
					}
				}
				$activeUserPosition = $this->_getActiveUserPosition($updatedUser->getUserPositionss());
				$activeUserPosition->setIsSuperior($lpUnit->getSupervisorLogoId() == $updatedUser->getExtId());
				
				if ($updatedUser->isNew() || $updatedUser->getUserRolessRelatedByToUserId()->count() == 0) {
					$userRole = new EmpevalUserRoles();
					$userRole->setRoles($userRoleDefault);
					$userRole->setUsersRelatedByToUserId($updatedUser);
					$userRole->save($con);
				}
				
				$updatedUser->save($con);
			}
			$con->commit();
		} catch (Exception $e) {
 			$con->rollBack();
 			throw $e;
 		}
		
	}
	
	private function _getActiveUserPosition($userPositions) {
		foreach ($userPositions as $userPosition) {
			if ($userPosition->getActive() == true)
				return $userPosition;
		}
		return null;
	}
	
	private function _newUserPosition($user, $position, $unit, $activeDate, $con = null) {
		$userPosition = new EmpevalUserPositions();
		$userPosition->setPositions($position);
		$userPosition->setUnits($unit);
		$userPosition->setActiveDate($activeDate->format('Y-m-d'));
		$userPosition->setIsFirst($user->isNew());
		$userPosition->setIsSuperior(false);
		$userPosition->setActive(true);
		return $userPosition;
		$userPosition->save($con);
	}
	
}